package com.tipfy.tmx;


class TiledObjectGroup
{
    /**
     * required, length > 0
     */
    public var name:String;

    /**
     * required, nonNegativeInteger
     */
    public var width:Int;

    /**
     * required, nonNegativeInteger
     */
    public var height:Int;

    /**
     *
     */
    public var objects:Array<TiledObject>;

    /**
     * Constructor.
     */
    public function new(xml:Xml)
    {
        // Attributes.
        this.name = TiledAttributes.getNonEmpty(xml, "name");
        this.width = TiledAttributes.getNonNegativeInt(xml, "width");
        this.height = TiledAttributes.getNonNegativeInt(xml, "height");

        // Child elements.
        for (elem in xml.elements())
        {
            switch (elem.nodeName)
            {
                case "object":
                    this._loadObject(elem);
                default:
                    throw "Invalid element <" + elem.nodeName +
                        "> in <objectgroup>";
            }
        }

        // Some elements must occur at least once.
        if (this.objects == null)
        {
            throw "Missing element <object>.";
        }
    }

    /**
     * <object>. minOccurs="1", maxOccurs="unbounded"
     */
    private function _loadObject(xml:Xml):Void
    {
        if (this.objects == null)
        {
            this.objects = new Array<TiledObject>();
        }

        this.objects.push(new TiledObject(xml));
    }
}
